<?php
/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 */
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);
ini_set('display_errors', '1');
/**/


// Load all required files for Camelot PHP Tools
require_once '../loader.php';


/**
  $options = array(
  'soap_version' => SOAP_1_1,
  'exceptions' => true,
  'trace' => 1
  );

  /* */
/**

  $client = new SoapClient("http://wcf.dev1.bendsoft.com/Camelot.svc?wsdl", $options);


  if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  try {
  $post_options = array(
  'name' => $_POST['name'],
  'email' => $_POST['email'],
  'company' => $_POST['company'],
  'message' => $_POST['message']);

  $post = $client->PostContactUs($post_options);
  } catch (Exception $exc) {
  pr($exc->getTraceAsString());
  pr($exc->getMessage());
  }
  }
  /* */
//  pr($client->GetServerTime()->GetServerTimeResult);
/**
  pr($client->GetServerTime()->GetServerTimeResult);
  pr($client->__getFunctions());
  pr($client->__getTypes());
  /* */
//pr($client->ExecuteCamelotXML(array("sql" => "Select * from `SharePoint Articles`"))->ExecuteCamelotXMLResult);
//pr($client->ExecuteCamelotXML(array("sql" => "Select * from `SharePoint Articles`"))->ExecuteCamelotXMLResult);

/*
  $sql = array('sql' => "INSERT INTO contactform (title,email,company,message)
  VALUES ('Eric Herlitz','eric@bendsoft.com','Bendsoft','A test message!')");
  $post = $client->ExecuteNonQuery($sql);
  echo($post->ExecuteNonQueryResult); // true (1) on success
  /* */
/**/

//pr($client->ExecuteCamelotXML(array("sql" => "Select * from `SharePoint Articles`", 'connString' => "sharepoint_cms", "compression" => true))->ExecuteCamelotXMLResult);
//SELECT LinkTitle AS Title, Article, Created FROM `SharePoint Articles` WHERE `Publish to external` = 'True'
// ﻿ExecuteScalar
// ExecuteNonQuery

/**/

$arguments = array(
    'sql' => "INSERT INTO contactform (title,email,company,message) VALUES ('John Doe','john.doe@example.com','Johns Company','A test message!')",
    'method' => '﻿ExecuteScalar',
    'connString' => 'sharepoint_customers',
    'sharedKey' => constant("WSDL_SHARED_KEY")
);


/**
  'sql' => "INSERT INTO contactform (title,email,company,message) VALUES ('John Doe','john.doe@example.com','Johns Company','A test message!')",
  'sql' => "DELETE FROM contactform WHERE ID = 73",
  'sql' => "UPDATE contactform SET title = 'Mrs Doe' WHERE title = 'John Doe'",
  /* */
/**/
/**
$SharePointNonQuery = new SharePointNonQuery(array(
    'sql' => "DELETE FROM contactform WHERE title = 'John Doe'",
    'method' => '﻿ExecuteScalar',
    'connString' => 'sharepoint_customers',
    'sharedKey' => constant("WSDL_SHARED_KEY")
));


$SharePointNonQuery = new SharePointNonQuery(array(
    'sql' => "INSERT INTO contactform (title,email,company,message) VALUES ('John Doe','john.doe@example.com','Johns Company','A test message!')",
    'method' => 'ExecuteNonQuery',
    'connString' => 'sharepoint_customers',
    'sharedKey' => constant("WSDL_SHARED_KEY")
));

$SharePointNonQuery = new SharePointNonQuery(array(
    'sql' => "UPDATE contactform SET Status = 'Accepted' WHERE `Contact Email` = 'John Doe@example.com'",
    'method' => 'ExecuteNonQuery',
    'connString' => 'sharepoint_connection',
    'sharedKey' => constant("WSDL_SHARED_KEY")
));



$SharePointScalarQuery = new SharePointNonQuery(array(
    'sql' => "INSERT INTO `My List` (title,email) VALUES ('John Doe','john.doe@example.com')",
    'method' => '﻿ExecuteScalar',
    'connString' => 'sharepoint_customers',
    'sharedKey' => constant("WSDL_SHARED_KEY")
));

if($SharePointScalarQuery->_result > 0) {
    echo "Last inserted ID is $SharePointScalarQuery->_result";
}

/**/


/**/
try {
    $SharePointQuery = new SharePointQuery(
        array(
            'sql' => "Select ID, LinkTitle as Title, Article, `Publish to external` AS Publish, Created from `SharePoint Articles`.Article",
            'compression' => false,
            'connString' => 'sharepoint_cms',
            'sharedKey' => constant("WSDL_SHARED_KEY")
        )
    );

    $SharePointResult = $SharePointQuery->CamelotSoap->_sorted;

require_once 'twig/lib/Twig/Autoloader.php';

Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('sp2007.html');
echo $template->render(array(
    'list_header' => $SharePointResult->_schema->display_name,
    'list_content' => $SharePointResult->_content
));
} catch (Exception $exc) {
    pr($exc->getTraceAsString());
    pr($exc->getMessage());
}
/* */
//      'list_header' => $SharePointResult->_schema->display_name,
//     'list_content' => $SharePointResult->_content
//      'article' => $SharePointResult->_content


/*
  $query = "SELECT LinkTitle AS Title, Article, Created FROM `SharePoint Articles` WHERE `Publish to external` = 'True'";
  $params = array("sql" => "Select * from `SharePoint Articles`", 'connString' => "sharepoint_cms", "compression" => true);
  $get = $client->ExecuteCamelotXML($params)->ExecuteCamelotXMLResult;

  $CamelotSoap = new CamelotSoapReader();
  $list = $CamelotSoap->get_list_data($get);
  /* */


/**
  echo "<hr /><h2>List some data</h2>";
  try {

  $query = "SELECT * FROM contactform";
  $sql = array('sql' => $query);
  $get = $client->ExecuteCamelotXML($sql)->ExecuteCamelotXMLResult;

  $file = constant("TEMP_DIR") . '\\' . md5($query) . '.xml';
  $fh = fopen($file, 'w');
  fwrite($fh, $get);
  fclose($fh);


  $CamelotSoap = new CamelotSoapReader();
  $list = $CamelotSoap->get_list_data($get);


  require_once 'twig/lib/Twig/Autoloader.php';


  Twig_Autoloader::register();
  $loader = new Twig_Loader_Filesystem('templates');
  $twig = new Twig_Environment($loader);
  $template = $twig->loadTemplate('sp2007.html');
  echo $template->render(array(
  'list_header' => $list->_schema->display_name,
  'list_content' => $list->_content
  ));
  } catch (Exception $exc) {
  pr($exc->getTraceAsString());
  pr($exc->getMessage());
  }
  /* */

/**
  echo "<hr /><h2>Insert new post</h2>";
  try {
  $sql = array('sql' => "SHOW TABLES");
  $get = $client->ExecuteCamelotXML($sql)->ExecuteCamelotXMLResult;

  $CamelotSoap = new CamelotSoapReader();
  $list = $CamelotSoap->get_list_data($get);


  require_once 'twig/lib/Twig/Autoloader.php';


  Twig_Autoloader::register();
  $loader = new Twig_Loader_Filesystem('templates');
  $twig = new Twig_Environment($loader);
  $template = $twig->loadTemplate('sp2007.html');
  echo $template->render(array(
  'list_header' => $list->_schema->display_name,
  'list_content' => $list->_content
  ));
  } catch (Exception $exc) {
  pr($exc->getTraceAsString());
  pr($exc->getMessage());
  }
  /* */
/*
  $options = array(
  'name' => "PHP User Again",
  'email' => "php@bendsoft.com",
  'company' => "Bendsoft",
  'message' => "My trial message");

  $post = $client->PostContactUs($options);
  echo($post->PostContactUsResult); // true (1) on success
  /* */

//pr($client);

/*
  [0] => PostContactUsResponse PostContactUs(PostContactUs $parameters)
  [1] => GetContactUsCamelotXMLResponse GetContactUsCamelotXML(GetContactUsCamelotXML $parameters)
  [2] => GetListCamelotXMLResponse GetListCamelotXML(GetListCamelotXML $parameters)
  [3] => ExecuteCamelotXMLResponse ExecuteCamelotXML(ExecuteCamelotXML $parameters)
  [4] => ExecuteScalarResponse ExecuteScalar(ExecuteScalar $parameters)
  [5] => ExecuteNonQueryResponse ExecuteNonQuery(ExecuteNonQuery $parameters)

 */
/*
  pr(chr(13) . chr(13) . "-----------------------------" . chr(13));
  pr("__getTypes() result:");
  pr($client->__getTypes());

  /*

  pr(chr(13) . chr(13) . "-----------------------------" . chr(13));
  pr($client->__getLastRequest());
  pr(chr(13) . chr(13) . "-----------------------------" . chr(13));
  pr($client->__getLastResponse());
  /* */



?>
<style>
    label {width: 100px; display: inline-block;}

</style>

<!--

<form method="post" action="?">
    <div>
        <label for="name">Name:</label> 
        <input type="text" id="name" name="name" />
    </div>
    <div>
        <label for="name">Email:</label> 
        <input type="text" id="email" name="email" />
    </div>
    <div>
        <label for="name">Company:</label> 
        <input type="text" id="company" name="company" />
    </div>
    <div>
        <label for="name">Message:</label> 
        <input type="text" id="message" name="message" />
    </div>
    <div>
        <input type="submit" />
    </div>

</form>
-->
